﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;

namespace TankIt
{
    public class Explosion
    {

        Texture2D t2dTexture;

        SoundEffect explode;

        float fFrameRate = 0.02f;
        float fElapsed = 0.0f;

        int iFrameOffsetX = 0;
        int iFrameOffsetY = 0;
        int iFrameWidth = 32;
        int iFrameHeight = 32;

        int iFrameCount = 1;
        int iCurrentFrame = 0;
        int iScreenX = 0;
        int iScreenY = 0;

        Vector2 position;


        bool bAnimating = false;


        public int X
        {
            get { return iScreenX; }
            set { iScreenX = value; }
        }

        public int Y
        {
            get { return iScreenY; }
            set { iScreenY = value; }
        }

        public int Frame
        {
            get { return iCurrentFrame; }
            set { iCurrentFrame = (int)MathHelper.Clamp(value, 0, iFrameCount); }
        }

        public float FrameLength
        {
            get { return fFrameRate; }
            set { fFrameRate = (float)Math.Max(value, 0f); }
        }

        public bool IsAnimating
        {
            get { return bAnimating; }
            set { bAnimating = value; }
        }


        public Explosion(Texture2D texture, int FrameOffsetX, int FrameOffsetY, int FrameWidth, int FrameHeight, int FrameCount, ContentManager content)
        {
            t2dTexture = texture;
            iFrameOffsetX = FrameOffsetX;
            iFrameOffsetY = FrameOffsetY;
            iFrameWidth = FrameWidth;
            iFrameHeight = FrameHeight;
            iFrameCount = FrameCount;
            explode = content.Load<SoundEffect>("Explosion");
        }

        public void setPosition(Vector2 pos)
        {
            position = pos;
            bAnimating = true;
            iCurrentFrame = 0;
            explode.Play();
        }


        public Microsoft.Xna.Framework.Rectangle GetSourceRect()
        {
            return new Microsoft.Xna.Framework.Rectangle(
            iFrameOffsetX + (iFrameWidth * iCurrentFrame),
            iFrameOffsetY,
            iFrameWidth,
            iFrameHeight);
        }

        public void Update(GameTimerEventArgs gametime)
        {
            if (bAnimating)
            {
                // Accumulate elapsed time...
                fElapsed += (float)gametime.TotalTime.Seconds;

                // Until it passes our frame length
                if (fElapsed > fFrameRate)
                {
                    // Increment the current frame, wrapping back to 0 at iFrameCount
                    iCurrentFrame = (iCurrentFrame + 1) % iFrameCount;
                    if (iCurrentFrame == 15) bAnimating = false;
                    // Reset the elapsed frame time.
                    fElapsed = 0.0f;
                    
                }
                
            }
        }


        public void Draw(SpriteBatch spriteBatch)
        {
            if(bAnimating)
            spriteBatch.Draw(
                t2dTexture,
                new Microsoft.Xna.Framework.Rectangle(
                  iScreenX + (int)position.X,
                  iScreenY + (int)position.Y,
                  iFrameWidth,
                  iFrameHeight),
                GetSourceRect(),
                Microsoft.Xna.Framework.Color.White);
        }


    }
}
